package weifei.live.simulation.im.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weifei.live.simulation.entity.ChatGroup;
import weifei.live.simulation.im.entity.GroupLiveStreamBulletCache;
import weifei.live.simulation.entity.GroupLiveStreamVideoTimeCache;
import weifei.live.simulation.im.GroupLiveStream;
import weifei.live.simulation.im.GroupLiveStreamManager;
import weifei.live.simulation.im.cmd.Bullet;

import java.util.Date;
import java.util.List;


@Service("GroupLiveService")
public class GroupLiveService
{
    // private boolean init = false;

    private final ChatGroup dao_group = new ChatGroup();
    @Autowired
    private GroupLiveStreamVideoTimeCache dao_group_live_stream_video_time_cache;
    @Autowired
    private GroupLiveStreamBulletCache dao_group_live_stream_bullet_cache;




    /*
    public void init()
    {
        if (this.init)
            return;
        var list = this.getGroupLiveStreamList();
        for (var l : list)
            this.addLiveStreamManager(l);
        this.init = true;
    }
    */


    public void addLiveStreamManager(GroupLiveStream groupLiveStream) {
        GroupLiveStreamManager.addLiveStreamManager(groupLiveStream.getGroupId(), groupLiveStream.getName());
    }
    public void removeLiveStreamManager(int groupId) {
        GroupLiveStreamManager.removeLiveStreamManager(groupId);
    }
    public GroupLiveStreamManager getLiveStreamManagerByGroupID(int groupId) {
        return GroupLiveStreamManager.getLiveStreamManagerByGroupID(groupId);
    }
    public GroupLiveStreamManager getLiveStreamManagerUserInside(int userId) {
        return GroupLiveStreamManager.getLiveStreamManagerByUserID(userId);
    }



    public List<GroupLiveStreamVideoTimeCache> getAllLiveStreamVideoTimeCache(int groupId)
    {
        QueryWrapper<GroupLiveStreamVideoTimeCache> qw_group_live_stream_video_time_cache = new QueryWrapper<>();
        qw_group_live_stream_video_time_cache.eq(GroupLiveStreamVideoTimeCache.Column.GroupID.getName(), groupId);
        return this.dao_group_live_stream_video_time_cache.selectList(qw_group_live_stream_video_time_cache);
    }


    public GroupLiveStreamVideoTimeCache getLiveStreamVideoTimeCacheOverTime(int groupId, Date time)
    {
        QueryWrapper<GroupLiveStreamVideoTimeCache> qw_group_live_stream_video_time_cache = new QueryWrapper<>();
        qw_group_live_stream_video_time_cache.eq(GroupLiveStreamVideoTimeCache.Column.GroupID.getName(), groupId);
        qw_group_live_stream_video_time_cache.le(GroupLiveStreamVideoTimeCache.Column.StartTime.getName(), time);
        qw_group_live_stream_video_time_cache.ge(GroupLiveStreamVideoTimeCache.Column.EndTime.getName(), time);
        return this.dao_group_live_stream_video_time_cache.selectOne(qw_group_live_stream_video_time_cache);
    }


    public GroupLiveStreamVideoTimeCache getLiveStreamVideoTimeCache(int groupId)
    {
        var now = new Date();
        return this.getLiveStreamVideoTimeCacheOverTime(groupId, now);
    }


    public String getLiveStreamVideoURLByGroupID(int groupId) {
        return this.getLiveStreamVideoTimeCache(groupId).getVideoURL();
    }


    public GroupLiveStream[] getGroupLiveStreams()
    {
        return GroupLiveStreamManager.getGroupLiveStreams();
    }


    public boolean isOwner(int userId, int groupId)
    {
        QueryWrapper<ChatGroup> qw_group = new QueryWrapper<>();
        qw_group.eq(ChatGroup.Column.ID.getName(), groupId);
        var g = this.dao_group.selectOne(qw_group);
        if (g == null) // 群聊不存在
            return false;
        return g.getOwnerId().equals(userId);
    }



    public void cacheBullet(Bullet bullet)
    {
        var sender_id = bullet.getSenderId();
        var group_id = bullet.getTargetId();
        var content = bullet.getContent();

        QueryWrapper<ChatGroup> qw_group = new QueryWrapper<>();
        qw_group.eq(ChatGroup.Column.ID.getName(), group_id);
        var g = this.dao_group.selectOne(qw_group);

        if (g == null)
            return;
        if (content == null)
            return;

        var cache = new GroupLiveStreamBulletCache(
                sender_id,
                group_id,
                content,
                bullet.getSendTime()
        );
        if (cache.insert())
            System.out.println("用户" + sender_id + " 发送到 群聊直播间" + group_id + "已缓存");

    }


    public List<GroupLiveStreamBulletCache> getBulletCacheList(int groupId)
    {
        QueryWrapper<GroupLiveStreamBulletCache> qw_bullet_cache = new QueryWrapper<>();
        qw_bullet_cache.eq(GroupLiveStreamBulletCache.Column.GroupID.getName(), groupId);
        return this.dao_group_live_stream_bullet_cache.selectList(qw_bullet_cache);
    }
}



